package com.ctg.behavior.calc.impala.builder.event;

import org.springframework.stereotype.Component;

import com.ctg.behavior.calc.common.CtCalcDate;
import com.ctg.behavior.calc.impala.report.event.CtCalcEventMeasureReport;
import com.ctg.behavior.common.consts.CtAggregatorConsts;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.consts.CtFieldConsts;
import com.ctg.behavior.utils.CtDateUtils;

@Component
public class CtEventMeasureDateScopeBuilder {

	public String doBuilder(CtCalcDate calcDate, CtCalcEventMeasureReport measure) {

		StringBuilder sbuf = new StringBuilder();
		
		// 事件类型
		String eventId = measure.getEventId();
		// 传入的属性或指标名
		String aggregator = measure.getAggregator().toLowerCase();

		// 写入事件名称
		if (!CtFieldConsts.FIELD_ANYTHING.equalsIgnoreCase(eventId)) {
			sbuf.append(" event = '").append(eventId).append("' AND ");
		}

		switch (aggregator) {
		// 总次数 用户数 人均数 合计总次数 合计用户数
		// 总和 均值 最大值 最小值
		// 人均值 去重数
		case CtAggregatorConsts.KEY_GENERAL:
		case CtAggregatorConsts.KEY_UNIQUE:
		case CtAggregatorConsts.KEY_AVERAGE:
		case CtAggregatorConsts.KEY_ROLLUP_COUNT:
		case CtAggregatorConsts.KEY_ROLLUP_UNIQ:

		case CtAggregatorConsts.KEY_SUM:
		case CtAggregatorConsts.KEY_AVG:
		case CtAggregatorConsts.KEY_MAX:
		case CtAggregatorConsts.KEY_MIN:

		case CtAggregatorConsts.KEY_UNIQAVG:
		case CtAggregatorConsts.KEY_UNIQCOUNT:
			// sbuf.append(" event_date = '").append(theDay).append("' ");
			this.appendEventDateLimit(sbuf, calcDate, measure.getUnit());
			return sbuf.toString();

		// 过去7天总次数 过去7天用户数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_7:
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_7:
			calcDate.setFromDate(CtDateUtils.addDate(calcDate.getFromDate(), -6));
			appendEventDateLimit(sbuf, calcDate, measure.getUnit());
			return sbuf.toString();

		// 过去30天总次数 过去30天用户数
		case CtAggregatorConsts.KEY_COUNT_LAST_DAY_30:
		case CtAggregatorConsts.KEY_UNIQ_LAST_DAY_30:
			calcDate.setFromDate(CtDateUtils.addDate(calcDate.getFromDate(), -29));
			appendEventDateLimit(sbuf, calcDate, measure.getUnit());
			return sbuf.toString();

		// 当月总次数 当月用户数
		case CtAggregatorConsts.KEY_UNIQ_CUR_MONTH:
			calcDate.setFromDate(CtDateUtils.formatMonthFirstDate(calcDate.getFromDate()));
			appendEventDateLimit(sbuf, calcDate, measure.getUnit());
			return sbuf.toString();

		}

		return "";
	}

	/**
	 * @param impalaSQL 需要拼接的字符串
	 * @param calcDate  计算时间范围
	 */
	private void appendEventDateLimit(StringBuilder impalaSQL, CtCalcDate calcDate, String calcUnit) {
		if (CtCalcConsts.UNIT_TYPE_DAY.equals(calcUnit) || CtCalcConsts.UNIT_TYPE_WEEK.equals(calcUnit)
				|| CtCalcConsts.UNIT_TYPE_MONTH.equals(calcUnit)) {
			String theFromDay = CtDateUtils.dateToString(calcDate.getFromDate(), CtDateUtils.YYYY_MM_DD);
			String theToDay = CtDateUtils.dateToString(calcDate.getToDate(), CtDateUtils.YYYY_MM_DD);
			impalaSQL.append(" event_date >= '").append(theFromDay).append("' ");
			impalaSQL.append(" AND ");
			impalaSQL.append(" event_date <= '").append(theToDay).append("' ");
		} else if (CtCalcConsts.UNIT_TYPE_MINUTE.equals(calcUnit) || CtCalcConsts.UNIT_TYPE_HOUR.equals(calcUnit)) {
			impalaSQL.append(" event_time >= '").append(calcDate.getFromDate().getTime()).append("' ");
			impalaSQL.append(" AND ");
			impalaSQL.append(" event_time <= '").append(calcDate.getToDate().getTime()).append("' ");
		} else {
			impalaSQL.append(" event_date = ''");
		}
	}

}
